package com.sgb.kjwl.viewmodel.pick;

import android.text.TextUtils;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.pick.PickSearchTender;
import com.sgb.kjwl.model.entity.pick.PreNoticeItemEntity;
import com.sgb.kjwl.repository.PickRepertory;
import com.sgb.kjwl.view.ui.fragment.pick.PickNoticingBidFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.areapopup.AddressEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PickNoticingModel extends BaseViewModel {
    private PickNoticingBidFragment pickNoticingBidFragment;
    private PickRepertory repertory;
    private HashMap map;
    private PickSelectEntity mAll;

    public PickNoticingModel(PickNoticingBidFragment pickNoticingBidFragment, PickRepertory repertory) {
        this.pickNoticingBidFragment = pickNoticingBidFragment;
        this.repertory = repertory;
        mAll = new PickSelectEntity();
        mAll.setNo("");
        mAll.setProjectscale("全部");
    }

    public void findBidNoticePage(String searchName, int total, int page,String type,String price,String areacode,String city,String sortMethod,String goodsType) {
        map = new HashMap();
//        map.put("areacode", repertory.getAreaCode());
        map.put("limit", 10);
        map.put("keyword", searchName);
        map.put("page", page);
        map.put("total", total);
        if(!TextUtils.isEmpty(type) && !type.equals("0")){
            map.put("type",type);
        }
        if(!TextUtils.isEmpty(price)){
            map.put("price",price);
        }
        if(!TextUtils.isEmpty(areacode)){
            map.put("province",areacode);
        }
        if(!TextUtils.isEmpty(city)){
            map.put("city",city);
        }
//        if(!TextUtils.isEmpty(sortMethod)){
//            map.put("sortMethod",sortMethod);
//        }
        if(!TextUtils.isEmpty(goodsType)){
            map.put("goodsType",goodsType);
        }
        DialogHelper.showProgressDialog2(pickNoticingBidFragment.getContext(), null, "加载中", 0, true, null,false);
        APIManager.getInstance().getPickAPI().findBidNoticePage(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<PreNoticeItemEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<PreNoticeItemEntity>>> data) {
                if (data == null || data.getState() == null)
                    return;
                if (data.getData().getList() == null){
                    data.getData().setList(new ArrayList<>());
                }
                if (data.getData().getList().size() != 0) {
                    pickNoticingBidFragment.getBidedData(data.getData().getList());
                } else {
                    pickNoticingBidFragment.empty();
                }
            }
        });
    }

    public void getProvinceData(int type) {
        DialogHelper.showProgressDialog2(pickNoticingBidFragment.getContext(), null, "加载中", 0, true, null,false);
        APIManager.getInstance().getPickAPI().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    List<PickSelectEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data.getData()) {
                        entities.add(new PickSelectEntity(datum.getNo(), datum.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    pickNoticingBidFragment.setChooseItemData(type, entities);
                    pickNoticingBidFragment.setProvinceDatas(data.getData());
                }
            }
        });
    }

    public void getAddressCityDataDialog(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData, String no) {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> scrDatum : scrData) {
            if (scrDatum.getId().equals(no)) {
                List<AddressEntity<List<AddressEntity>>> children = scrDatum.getChildren();
                if (children != null && children.size() > 0) {
                    List<PickSelectEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : children) {
                        entities.add(new PickSelectEntity(child.getNo(), child.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    pickNoticingBidFragment.setCityDatas(entities);
                }
            }
        }
    }


    public void getAddressCityData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData, String no, int type) {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> scrDatum : scrData) {
            if (scrDatum.getId().equals(no)) {
                List<AddressEntity<List<AddressEntity>>> children = scrDatum.getChildren();
                if (children != null && children.size() > 0) {
                    List<PickSelectEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : children) {
                        entities.add(new PickSelectEntity(child.getNo(), child.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    pickNoticingBidFragment.setChooseItemData(type, entities);
                }
            }
        }
    }

    //新增类型过滤接口
    public void getTenderGoodsCategoryAllList(String category) {
        DialogHelper.showProgressDialog2(pickNoticingBidFragment.getContext(), null, "数据加载中...", 0, false, null,false);
        Map map = new HashMap();
        map.put("type", category);
        APIManager.getInstance().getPickAPI().getTenderGoodsCategoryAllList(map).enqueue(new Callback<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> call, Response<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    pickNoticingBidFragment.setTenderGoodsCategoryAllList(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getFilterTypeData() {
        DialogHelper.showProgressDialog2(pickNoticingBidFragment.getContext(), null, "数据加载中...", 0, false, null,false);
        APIManager.getInstance().getPickAPI().tenderNewTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    pickNoticingBidFragment.setTypeDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getSortTypeData() {
        APIManager.getInstance().getPickAPI().getBidderNoticeSortSelect().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    pickNoticingBidFragment.setSortDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {

            }
        });
    }

    public void getSelecteScaleData() {
        DialogHelper.showProgressDialog2(pickNoticingBidFragment.getContext(), null, "数据加载中...", 0, false, null,false);
        APIManager.getInstance().getPickAPI().getProjectMoney().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    pickNoticingBidFragment.setPriceDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
